from .champion import Guard
from typing import Dict, Any, Optional


class ZhenJi(Guard):
    """甄姬 - 死亡抽牌"""
    
    def __init__(self):
        super().__init__(
            card_id="zhen_ji",
            name="Zhen Ji",
            image_path="Zhen Ji.jpg",
            description="Guard: Must target a unit to guard when summoned.\nDeath: Draw 1 card",
            cost=2,
            attack=1,
            hp=2
        )
    
    def on_death(self, game_state, player_id: str):
        """死亡时从抽牌堆抽1张牌"""
        if len(game_state.draw_piles[player_id]) > 0:
            # 抽一张牌到手牌
            drawn_card = game_state.draw_piles[player_id].pop(0)
            game_state.hands[player_id].append(drawn_card)


class SunCe(Guard):
    """孙策 - 基础守卫"""
    
    def __init__(self):
        super().__init__(
            card_id="sun_ce",
            name="Sun Ce",
            image_path="Sun Ce.jpg",
            description="Guard: Must target a unit to guard when summoned",
            cost=0,
            attack=1,
            hp=1
        )


class ZhouYu(Guard):
    """周瑜 - 基础守卫"""
    
    def __init__(self):
        super().__init__(
            card_id="zhou_yu",
            name="Zhou Yu",
            image_path="Zhou Yu.jpg",
            description="Guard: Must target a unit to guard when summoned",
            cost=0,
            attack=2,
            hp=2
        )


class ZhangLiang(Guard):
    """张良 - 入场时若PP上限>=7，回复己方召唤师2血"""
    
    def __init__(self):
        super().__init__(
            card_id="zhang_liang",
            name="Zhang Liang",
            image_path="Zhang Liang.jpg",
            description="Guard: Must target a unit to guard when summoned.\nOn summon: If max PP >= 7, restore 2 HP to your summoner",
            cost=3,
            attack=2,
            hp=3
        )
    
    def on_summon(self, game_state, player_id: str):
        """入场时若PP上限>=7，回复己方召唤师2血"""
        if game_state.max_pp[player_id] >= 7:
            summoner = game_state.get_summoner(player_id)
            summoner.heal(2)


class HuaMulan(Guard):
    """花木兰 - 基础守卫"""
    
    def __init__(self):
        super().__init__(
            card_id="hua_mulan",
            name="Hua Mulan",
            image_path="Hua Mulan.jpg",
            description="Guard: Must target a unit to guard when summoned",
            cost=6,
            attack=5,
            hp=10
        )


class LiuBei(Guard):
    """刘备 - 死亡时召唤一个Dragon"""
    
    def __init__(self):
        super().__init__(
            card_id="liu_bei",
            name="Liu Bei",
            image_path="Liu Bei.jpg",
            description="Guard: Must target a unit to guard when summoned.\nDeath: Summon a Dragon",
            cost=8,
            attack=6,
            hp=6
        )
    
    def on_death(self, game_state, player_id: str):
        """死亡时召唤一个Dragon"""
        # 寻找空位召唤Dragon
        battlefield = game_state.battlefields[player_id]
        for i, slot in enumerate(battlefield):
            if slot is None:
                # 创建Dragon实例
                from .assassin import Dragon
                dragon = Dragon()
                import uuid
                dragon.instance_id = f"{player_id}_{i}_dragon_{uuid.uuid4().hex[:8]}"
                dragon.position = i
                battlefield[i] = dragon
                break


class LuBan(Guard):
    """鲁班 - 基础守卫"""
    
    def __init__(self):
        super().__init__(
            card_id="lu_ban",
            name="Lu Ban",
            image_path="Lu Ban.jpg",
            description="Guard: Must target a unit to guard when summoned",
            cost=2,
            attack=1,
            hp=5
        )


class DiaoChan(Guard):
    """貂蝉 - 入场时恢复己方召唤师4HP"""
    
    def __init__(self):
        super().__init__(
            card_id="diao_chan",
            name="Diao Chan",
            image_path="Diao Chan.jpg",
            description="Guard: Must target a unit to guard when summoned.\nOn summon: Restore 4 HP to your summoner",
            cost=5,
            attack=3,
            hp=7
        )
    
    def on_summon(self, game_state, player_id: str):
        """入场时恢复己方召唤师4HP"""
        summoner = game_state.get_summoner(player_id)
        summoner.heal(4)


class LiShuwen(Guard):
    """李书文 - 召唤时使所有敌方单位返回敌方手牌"""
    
    def __init__(self):
        super().__init__(
            card_id="li_shuwen",
            name="Li Shuwen",
            image_path="Li Shuwen.jpg",
            description="Guard: Must target a unit to guard when summoned.\nOn summon: Return all enemy units to enemy hand",
            cost=10,
            attack=5,
            hp=8
        )
    
    def on_summon(self, game_state, player_id: str):
        """召唤时使所有敌方单位返回敌方手牌"""
        # 遍历所有敌方玩家
        for enemy_id in game_state.players:
            if enemy_id == player_id:
                continue
            
            # 获取敌方战场
            enemy_battlefield = game_state.battlefields[enemy_id]
            
            # 收集所有敌方单位并清空战场
            returned_units = []
            for i, champion in enumerate(enemy_battlefield):
                if champion and champion.is_alive():
                    card_id = champion.card_id
                    returned_units.append(card_id)
                    enemy_battlefield[i] = None
            
            # 将单位返回到敌方手牌
            for card_id in returned_units:
                # 手牌限制：最多7张
                if len(game_state.hands[enemy_id]) < 9:
                    game_state.hands[enemy_id].append(card_id)
                else:
                    # 如果手牌满了，放入抽牌堆顶
                    game_state.draw_piles[enemy_id].insert(0, card_id)


class LiuBang(Guard):
    """刘邦 - 死亡时对所有敌方单位造成2点伤害"""
    
    def __init__(self):
        super().__init__(
            card_id="liu_bang",
            name="Liu Bang",
            image_path="Liu Bang.jpg",
            description="Guard: Must target a unit to guard when summoned.\nDeath: Deal 2 damage to all enemy units",
            cost=2,
            attack=0,
            hp=6
        )
    
    def on_death(self, game_state, player_id: str):
        """死亡时对所有敌方单位造成2点伤害"""
        # 遍历所有敌方玩家
        for enemy_id in game_state.players:
            if enemy_id == player_id:
                continue
            
            # 获取敌方战场
            enemy_battlefield = game_state.battlefields[enemy_id]
            
            # 对每个敌方单位造成2点伤害
            for champion in enemy_battlefield:
                if champion and champion.is_alive():
                    champion_dead = champion.take_damage(2)
                    
                    # 如果单位死亡，从战场移除
                    if champion_dead:
                        game_state._remove_dead_champion(champion.instance_id)
